/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import { jsonBigint } from "./index.js"


let input = '{"bigValue":9223372036854775807,"SmallValue":123}'
let storeAsStringInput = '{ "key": 1234567890123456789 }';
let useNativeBigIntInput = '{ "key": 993143214321423154315154321 }';
let alwaysParseAsBigInput = '{ "key": 12312312312 }';
const forNumber = 10000
const bigNumber = 50000
// Test whether native json_bigint conversion supports bigint
function parse_test_01() {
    let parseValue;
    let bigint = jsonBigint();
    let startTime1 = Date.now()
    for (let i = 0; i < forNumber; i++) {
        parseValue = bigint.parse(input);
    }
    let endTime1 = Date.now()
    if (parseValue.bigValue.toString() != '9223372036854775807') {
        throw new Error('Inconsistent with expected results')
    }
    print(`jsonBigint_Parse: ${endTime1 - startTime1} ms`)
    let stringifyValue 
    stringifyValue = bigint.stringify(bigint.parse(input));
}

function stringify_test() {
    let parseValue;
    let bigint = jsonBigint();
    parseValue = bigint.parse(input);
    let stringifyValue; 
    let startTime2 = Date.now()
    for (let k = 0; k < forNumber; k++) {
        stringifyValue = bigint.stringify(parseValue);
    }
    let endTime2 = Date.now()
    if (stringifyValue.length != 49) {
        throw new Error('Inconsistent with expected results')
    }
    print(`jsonBigint_Stringify: ${endTime2 - startTime2} ms`)
}

// add strict：true Specify the resolution to be 'strict', and if there are duplicate key values, they will be invalidated when such duplicate keys occur, thus providing early warning of possible lost information.
function parse_test_02() {
    let failsInput;
    let bigint = jsonBigint({ strict: true });
    var startTime = Date.now()
    for (let i = 0; i < forNumber; i++) {
        failsInput = bigint.parse(input);
    }
    var endTime = Date.now()
    if (!(failsInput != null)) {
        throw new Error('Inconsistent with expected results')
    }
    print(`jsonBigint_Parse_strict: ${endTime - startTime} ms`)
}

// bigint Store As String
function parse_test_03() {
    let withString;
    let bigint = jsonBigint({ storeAsString: true });
    let startTime = Date.now()
    for (let i = 0; i < forNumber; i++) {
        withString = bigint.parse(storeAsStringInput);
    }
    let endTime = Date.now()
    if (withString.key != '1234567890123456789') {
        throw new Error('Inconsistent with expected results')
    }
    print(`jsonBigint_Parse_storeAsString: ${endTime - startTime} ms`)
}

// use Native BigInt not bignumber.js
function parse_test_04() {
    let JSONbigNative; 
    let bigint = jsonBigint({ useNativeBigInt: true });
    let startTime = Date.now()
    for (let i = 0; i < forNumber; i++) {
        JSONbigNative = bigint.parse(useNativeBigIntInput);
    }
    let endTime = Date.now()
    if (JSONbigNative.key != '993143214321423154315154321') {
        throw new Error('Inconsistent with expected results')
    }
    print(`jsonBigint_Parse_useNativeBigInt: ${endTime - startTime} ms`)
}

// always Parse As BigNumber。
function parse_test_05() {
    let JSONbigAlways;
    let bigint = jsonBigint({ alwaysParseAsBig: true });
    let startTime = Date.now()
    for (let i = 0; i < forNumber; i++) {
        JSONbigAlways = bigint.parse(alwaysParseAsBigInput);
    }
    let endTime = Date.now()
    if (JSONbigAlways.key != '12312312312') {
        throw new Error('Inconsistent with expected results')
    }

    print(`jsonBigint_Parse_alwaysParseAsBig: ${endTime - startTime} ms`)
}

function jsonBigint_test() {
    let bigint;
    let startTime1 = Date.now()
    for (let i = 0; i < bigNumber; i++) {
        bigint = jsonBigint();
    }
    let endTime1 = Date.now()
    let parseValue = bigint.parse(input);

    if (parseValue.bigValue.toString() != '9223372036854775807') {
        throw new Error('Inconsistent with expected results')
    }
    print(`jsonBigint_jsonBigint: ${endTime1 - startTime1} ms`)
}

parse_test_01()
stringify_test()
parse_test_02()
parse_test_03()
parse_test_04()
parse_test_05()
jsonBigint_test()